package com.sunda.spmsoversea.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsoversea.dto.WhsOperateLogSearchDTO;
import com.sunda.spmsoversea.entity.WhsOperateLog;
import com.sunda.spmsoversea.mapper.WhsOperateLogMapper;
import com.sunda.spmsoversea.service.IWhsOperateLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 库存操作日志表 服务实现类
 * </p>
 *
 * @author Wayne
 * @since 2021-09-10
 */
@Service("overseaWhsOperateLogServiceImpl")
@Slf4j
public class WhsOperateLogServiceImpl extends ServiceImpl<WhsOperateLogMapper, WhsOperateLog> implements IWhsOperateLogService {

    /**
     海外仓只有储位库存日志 documentType = 4
     * */
    @Override
    public void insertWhsOperateLog(String uuidStorage, String materialNo, double quantity, String operateType, String relateDocumentNumber,
                                    String comments, String userId, String remark, String werks, String whsLocationCode, String movementType) {
        WhsOperateLog whsOperateLog = new WhsOperateLog();
        whsOperateLog.setLogId(UUID.randomUUID().toString().replaceAll("-", "").toLowerCase());
        whsOperateLog.setDocumentType("4");
        whsOperateLog.setUuidStorage(uuidStorage);
        whsOperateLog.setMaterialNo(materialNo);
        whsOperateLog.setQuantity(quantity);
        whsOperateLog.setOperationType(operateType);
        whsOperateLog.setRelateDocumentNumber(relateDocumentNumber);
        whsOperateLog.setRemark(remark);
        whsOperateLog.setComments(comments);
        whsOperateLog.setUserId(userId);
        whsOperateLog.setWerks(werks);
        whsOperateLog.setWhsLocationCode(whsLocationCode);
        whsOperateLog.setMovementType(movementType);
        this.baseMapper.insert(whsOperateLog);
    }

    /** 海外仓记录箱码到货日志，仅记录箱码到货，仓库库存日志 documentType = 3 */
    @Override
    public void recordGetBoxNote(String boxNote, String operationType, String deliveryNoteSap, String relateDocumentNumber, String comments,
                                 String userId, String remark, String werks, String whsLocationCode, String movementType) {
        WhsOperateLog whsOperateLog = new WhsOperateLog();
        whsOperateLog.setLogId(UUID.randomUUID().toString().replaceAll("-", "").toLowerCase());
        whsOperateLog.setDocumentType("3");
        whsOperateLog.setBoxNote(boxNote);
        whsOperateLog.setQuantity(1.0);
        whsOperateLog.setOperationType(operationType);
        whsOperateLog.setDeliveryNoteSap(deliveryNoteSap);
        whsOperateLog.setRelateDocumentNumber(relateDocumentNumber);
        whsOperateLog.setRemark(remark);
        whsOperateLog.setComments(comments);
        whsOperateLog.setUserId(userId);
        whsOperateLog.setWerks(werks);
        whsOperateLog.setWhsLocationCode(whsLocationCode);
        whsOperateLog.setMovementType(movementType);
        this.baseMapper.insert(whsOperateLog);
    }

    /** 海外仓日志查询，查询条件控制 documentType = 4，返回结构带关联单据号，分页 */
    @Override
    public ResponseResult getWhsOperateLog(WhsOperateLogSearchDTO operateLogSearchDTO) {
        IPage iPage = operateLogSearchDTO.getPage();
        List<Map<String, Object>> mapList = this.baseMapper.getWhsOperateLog(iPage, operateLogSearchDTO);
        return ResponseResult.success().add("whsOperateLogList", iPage.setRecords(mapList));
    }

    /** 海外仓日志查询，查询条件控制 documentType = 4，返回结构带关联单据号，不分页 */
    @Override
    public ResponseResult getWhsOperateLogList(WhsOperateLogSearchDTO operateLogSearchDTO) {
        List<Map<String, Object>> mapList = this.baseMapper.getWhsOperateLogList(operateLogSearchDTO);
        return ResponseResult.success().add("whsOperateLogList", mapList);
    }

    /** 脏数据排查 20220310 */
    @Override
    public ResponseResult checkErrorDate() {
        List<Map<String, Object>> reData = new ArrayList<>();
        List<Map<String, Object>> dataList = this.baseMapper.getWhsOperateLogs();
        if (dataList.size() > 0){
            log.info("============dataList.size = " + dataList.size());
            for (int i = 0; i < dataList.size(); i ++){
                Map<String, Object> map = dataList.get(i);
                String werks = map.get("werks").toString();
                String materialNo = map.get("materialNo").toString();
                double totalQuantity = Double.parseDouble(map.get("totalQuantity").toString());

                double tempQuantity = 0;
                List<Map<String, Object>> whsOperateLogs = this.baseMapper.getWOL(werks, materialNo);
                for (Map<String, Object> logMap : whsOperateLogs){
                    double quantity = Double.parseDouble(logMap.get("quantity").toString());
                    String operateType = logMap.get("operationType").toString();
                    if ("3".equals(operateType)){
                        quantity = -quantity;
                    }
                    tempQuantity = tempQuantity + quantity;
                }

                if (totalQuantity - tempQuantity != 0){
                    log.info("==========" + werks + "   " + materialNo + "    " + totalQuantity + "    " + tempQuantity);
                    Map<String, Object> reMap = new HashMap<>();
                    reMap.put("werks", werks);
                    reMap.put("materialNo", materialNo);
                    reMap.put("totalQuantity", totalQuantity);
                    reMap.put("tempQuantity", tempQuantity);
                    reData.add(reMap);
                }
            }
        }
        return ResponseResult.success().add("errorData", reData);
    }
}
